//
//  ProductNameViewModel.m
//  GLGW_Shoping
//
//  Created by yang on 2018/11/13.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "ProductNameViewModel.h"
#import "ProductNameListTableViewCell.h"

@interface ProductNameViewModel ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView * selfTableView;
    NSArray * dataSource;
}
@end

@implementation ProductNameViewModel

-(instancetype)initWithViewToViewModel:(UITableView *)tableView theSuperView:(UIView *)superView{
    if (self = [super initWithViewToViewModel:tableView theSuperView:superView]) {
        tableView.mj_footer.hidden = YES;
        tableView.mj_header.hidden = YES;
        tableView.tableHeaderView = [self headerView];
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.showsVerticalScrollIndicator = YES;
        tableView.rowHeight = kHeightProportion_In_iPhoneTriploid(44);
        [superView addSubview:tableView];
        selfTableView = tableView;
    }
    return self;
}
-(UIView *)headerView{
    UIView * headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, kHeightProportion_In_iPhoneTriploid(10))];
    headerView.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
    return headerView;
}

#pragma mark -- set
-(void)setPub_type:(PublishSelectList)pub_type{
    _pub_type = pub_type;
    [self requestListData:_pub_type];
}

#pragma mark -- 数据请求
-(void)requestListData:(PublishSelectList)p_type{
    NSString * requStr;
    switch (p_type) {
        case PublishSelectList_GoodsName:{
            requStr = kBasicsProduct;
        }
            break;
        case PublishSelectList_Material:{
            requStr = kBasicsMaterial;
        }
            break;
        case PublishSelectList_Spec:{
            requStr = kBasicsSpecifications;
        }
            break;
        case PublishSelectList_Stell:{
            requStr = kbasicsfactory;
        }
            break;
            
        default:
            break;
    }
    [GLGWRequestTool POST:[NSString stringWithFormat:@"%@%@",kApiPrefix,requStr] lodingView:nil lodingTextString:@"" isLoding:YES parameters:@{} success:^(id  _Nullable response) {
       PublishListModel * p_model = [PublishListModel yy_modelWithJSON:response];
        if (p_model.code == 0) {
            self->dataSource = p_model.data;
            [self->selfTableView reloadData];
        }
    } failure:^(NSError * _Nullable error) {
        
    }];
}
#pragma mark -- UITableViewDelegate,UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return dataSource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ProductNameListTableViewCell * cell = [ProductNameListTableViewCell cellWithTableView:tableView];
    PublishListModel_Data * dataModel = dataSource[indexPath.row];
    if (_pub_type == PublishSelectList_GoodsName) {
        cell.leftStr = dataModel.productName;
    }else if (_pub_type == PublishSelectList_Material){
        cell.leftStr = dataModel.materialName;
    }else if (_pub_type == PublishSelectList_Spec){
        cell.leftStr = dataModel.specifications;
    }else{
        cell.leftStr = dataModel.factoryName;
    }
    cell.showPush = YES;
    [cell setSepartionLineWith:lineModel_Half];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.delegate && [self.delegate respondsToSelector:@selector(productDidSelectRowAtIndexPath:)]) {
        [self.delegate productDidSelectRowAtIndexPath:dataSource[indexPath.row]];
    }
}
@end
